package com.shuoyue.ycgk.ui.common;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.DayTaget;
import com.shuoyue.ycgk.entity.QuestionNote;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import io.reactivex.Observable;

public interface NoteContract {
    interface View extends BaseView {
        void setNotes(ListWithPage<QuestionNote> list);

        void addNoteSuc();

        void praiseResult(QuestionNote actionId, boolean isPraised);
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void saveNote(int questionId, String notes) {
            apply(model.saveNote(questionId, notes))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> dayTagetOptional) {
                            super.onNext(dayTagetOptional);
                            mView.addNoteSuc();
                        }
                    });
        }

        public void getQuestionNotes(int questionId, int page) {
            apply(model.getQuestionNotes(questionId, page))
                    .subscribe(new ApiSubscriber<Optional<ListWithPage<QuestionNote>>>(mView, this) {
                        @Override
                        public void onNext(Optional<ListWithPage<QuestionNote>> dataOp) {
                            super.onNext(dataOp);
                            mView.setNotes(dataOp.getIncludeNull());
                        }
                    });
        }

        public void praiseNote(QuestionNote questionNote, int isAdd) {
            apply(model.praiseNote(questionNote.getId(), isAdd))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> dataOp) {
                            super.onNext(dataOp);
                            mView.praiseResult(questionNote, isAdd == 1);
                        }
                    });
        }

    }

    class Model extends BaseModel {
        Observable<BaseResult<String>> saveNote(int questionId, String notes) {
            return RetrofitClient.getInstance().getApi().saveNote(questionId, notes);
        }

        Observable<BaseResult<ListWithPage<QuestionNote>>> getQuestionNotes(int questionId, int page) {
            return RetrofitClient.getInstance().getApi().getQuestionNotes(questionId, page);
        }

        Observable<BaseResult<String>> praiseNote(int actionId, int isAdd) {
            return RetrofitClient.getInstance().getApi().praiseNote(actionId, isAdd);
        }
    }
}
